﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;
using Petroleum.UC.Public;
using System.Text.RegularExpressions;
using Petroleum.DB.Model;
using Petroleum.Model;
using Petroleum.Tool;
using Newtonsoft.Json;
using System.Data.SQLite;
using HZH_Controls.Forms;

namespace Petroleum.UC
{
    public partial class Step3Table : UserControl
    {
        FormMain main = new FormMain();
        List<Trajectory> TrajectoryList { get; set; } = new List<Trajectory>();
        bool hassave = false;
        public Step3Table(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            this.dgv.AutoGenerateColumns = false;
        }
        //下一步
        private void ucBtnExt4_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            hassave = true;
            main.AddControl(new Step3Img(main));
        }

        #region ==重写键盘事件==
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //在检测到按Ctrl+V键后，系统无法复制多单元格数据，重写ProcessCmdKey方法，屏蔽系统粘贴事件，使用自定义粘贴事件，在事件中对剪贴板的HTML格式进行处理，获取表数据，更新DataGrid控件内容

            if (keyData == (Keys.V | Keys.Control) && this.dgv.SelectedCells.Count > 0 && !this.dgv.SelectedCells[0].IsInEditMode)
            {
                DGVHelper.CopyData(this.dgv);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                dgv.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font, rect,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        #endregion
        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="RowIndex"></param>
        /// <param name="CellName"></param>
        /// <returns></returns>
        decimal GetCell(int RowIndex, string CellName)
        {
            string value = dgv.Rows[RowIndex].Cells[CellName].Value.ToString();
            return string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value.Trim());
        }
        void InitCell(int RowIndex, string CellName)
        {
            dgv.Rows[RowIndex].Cells[CellName].Value = "";
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step2Img(main));
        }
        void SaveData()
        {
            TrajectoryList.Clear();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                var val = dgv.Rows[i].Cells["MD"].Value;
                if (val == null || val == "")
                    continue;
                Trajectory trajectory = new Trajectory()
                {
                    MD = GetCell(i, "MD"),
                    CL = GetCell(i, "CL"),
                    Inc = GetCell(i, "Inc"),
                    Azi = GetCell(i, "Azi"),
                    TVD = GetCell(i, "TVD"),
                    NS = GetCell(i, "NS"),
                    EW = GetCell(i, "EW"),
                    VSec = GetCell(i, "VSec"),
                    Dogleg = GetCell(i, "Dogleg"),
                    MapE = GetCell(i, "MapE"),
                    MapN = GetCell(i, "MapN"),
                    //IsTarget = dgv.Rows[i].DefaultCellStyle.BackColor == Color.Pink ? 1 : 0
                    IsTarget = dgv.Rows[i].Cells["Target"].Value + ""
                };
                if (trajectory.MD != AppConfig.DefaultDecimal)
                {
                    TrajectoryList.Add(trajectory);
                }
            }
            Session.WellTrajectory.TrajectoryList = TrajectoryList;
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = main.projects.Where(n => n.TableName == TableNames.WellTrajectory.ToString()).FirstOrDefault();
            if (casing != null)
            {
                Session.WellTrajectory = JsonConvert.DeserializeObject<WellTrajectory>(casing.TableData);
                TrajectoryList = Session.WellTrajectory.TrajectoryList;
                AddRow();
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave)
            {
                SaveData();
            }
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.WellTrajectory.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.WellTrajectory.ToString(), JsonConvert.SerializeObject(Session.WellTrajectory));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.WellTrajectory.ToString(), JsonConvert.SerializeObject(Session.WellTrajectory), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count > 0)
            {
                dgv.Rows.Remove(dgv.SelectedRows[0]);
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        void AddRow()
        {
            foreach (var p in TrajectoryList)
            {
                int index = this.dgv.Rows.Add();
                this.dgv.Rows[index].Cells[0].Value = p.MD;
                this.dgv.Rows[index].Cells[1].Value = p.CL;
                this.dgv.Rows[index].Cells[2].Value = p.Inc;
                this.dgv.Rows[index].Cells[3].Value = p.Azi;
                this.dgv.Rows[index].Cells[4].Value = p.TVD;
                this.dgv.Rows[index].Cells[5].Value = p.NS;
                this.dgv.Rows[index].Cells[6].Value = p.EW;
                this.dgv.Rows[index].Cells[7].Value = p.VSec;
                this.dgv.Rows[index].Cells[8].Value = p.Dogleg;
                this.dgv.Rows[index].Cells[9].Value = p.MapN;
                this.dgv.Rows[index].Cells[10].Value = p.MapE;
                this.dgv.Rows[index].Cells[11].Value = p.IsTarget;
            }
        }
    }
}
